<template>
  <el-main>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="房间名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="酒店名称" prop="hid">
        <el-select v-model="ruleForm.hid"  :placeholder="this.hname" disabled>
          <el-option v-for="l in this.hotelData" :label="l.name" :value="l.id" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="床型" prop="bed">
        <el-select v-model="ruleForm.bed" placeholder="请选择品牌">
          <el-option v-for="l in this.bedData" :label="l.name" :value="l.level"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="网络服务" prop="mesh">
        <el-select v-model="ruleForm.mesh" placeholder="请选择品牌">
          <el-option v-for="l in this.wlData" :label="l.name" :value="l.level"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="房间图片" prop="pic">
        <el-upload
            class="avatar-uploader"
            action="http://api.com/hotel/tofile"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
          <img v-if="this.ruleForm.pic" width="120" height="100" :src="'http://api.com/'+this.ruleForm.pic" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-main>
</template>

<script>
export default {

  data() {
    return{
      hotelData:[],
      ruleForm: {
        hid:'',
        pic:null
      },
      rules: {
        name: [
          { required: true, message: '不可为空', trigger: 'blur' },
        ],
        hid: [
          { required: true, message: '不可为空', trigger: 'blur' },
        ],
        bed: [
          { required: true, message: '不可为空', trigger: 'blur' },
        ],
        mesh: [
          { required: true, message: '不可为空', trigger: 'blur' },
        ],
        pic: [
          { required: true, message: '不可为空', trigger: 'blur' },
        ]
      },
      bedData:[{
        name:'大床',
        level:'0'
      },{
        name:'中床',
        level:'1'
      },{
        name:'小床',
        level:'3'
      }],
      wlData:[{
        name:'无宽带',
        level:'A'
      },{
        name:'免费宽带',
        level:'B'
      },{
        name:'收费宽带',
        level:'C'
      }],
      hname:null

    }
  },
  methods:{
    //去发布
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios.post(this.$baseUrl+"/hotel/addtenant",this.ruleForm).then(res=>{
            console.log("添加的房客信息:", res);
            if (res.data.errCode == 500) {
              this.$message.error(res.data.message);
            } else {
              this.$message.success("添加客房成功");
              this.$router.push("/hotel");
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //上传
    handleAvatarSuccess(res, file) {
      this.ruleForm.pic = res.fileName;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 4;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 4MB!');
      }
      return isJPG && isLt2M;
    },
    gethotols() {
      this.axios.get(this.$baseUrl+"/hotel/gethotel").then(res=>{
        console.log("酒店:", res);
        this.hotelData = res.data;
      })
    }
  },
  created() {
    this.gethotols();
    let params = this.$route.params;
    let parse = JSON.parse(params.hotels);
    this.ruleForm.hid = parse.id;
    this.hname = parse.name;
  }
}
</script>

<style scoped>

</style>